package com.tuanbiao.ui.log;


import android.content.Context;
import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.abiao.lib_utils.ViewUtils;
import com.hzjq.data.entity.BlastEntity;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;
import com.tuanbiao.R;
import com.tuanbiao.base.mvp.BasePresenterActivity;
import com.tuanbiao.controllers.LogController;
import com.tuanbiao.databinding.ActivityBlastLogBackupBinding;
import com.tuanbiao.models.LogModel;
import com.tuanbiao.presenter.BlastLogBackupPresenter;
import com.tuanbiao.utils.ClickUtil;
import com.tuanbiao.utils.LibCollections;

import java.util.List;

public class BlastBackupActivity extends BasePresenterActivity<BlastLogBackupPresenter, ActivityBlastLogBackupBinding> implements LogController.BackupView {
    private BlastCopyAdapter blastCopyAdapter;

    public static void start(Context context) {
        Intent starter = new Intent(context, BlastBackupActivity.class);
        context.startActivity(starter);
    }

    @Override
    protected ActivityBlastLogBackupBinding getBindingView() {
        return ActivityBlastLogBackupBinding.inflate(getLayoutInflater());
    }

    @Override
    protected void initView() {
        binding.ivToolbarBack.setOnClickListener(getBackListener());
        binding.rvContent.setLayoutManager(new LinearLayoutManager(getActivity()));
        blastCopyAdapter = new BlastCopyAdapter();
        blastCopyAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (ClickUtil.isFastClick()) {
                return;
            }
            BlastEntity entity = (BlastEntity) adapter.getItem(position);
            mPresenter.restore(entity);
        });
        binding.rvContent.setAdapter(blastCopyAdapter);
        binding.srlContent.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.loadMore();
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.refresh();
            }
        });
        binding.srlContent.autoRefresh();
    }


    @Override
    protected BlastLogBackupPresenter createPresenter() {
        return new BlastLogBackupPresenter(new LogModel(), this);
    }

    @Override
    public void setItems(List<BlastEntity> entities) {
        binding.srlContent.finishRefresh();
        if (LibCollections.isEmpty(entities)) {
            blastCopyAdapter.setEmptyView(ViewUtils.layoutId2View(R.layout.include_no_data));
        }
        blastCopyAdapter.setNewInstance(entities);
    }

    @Override
    public void addItems(List<BlastEntity> blastEntities) {
        if (LibCollections.isEmpty(blastEntities))
            binding.srlContent.finishLoadMoreWithNoMoreData();
        else blastCopyAdapter.addData(blastEntities);
    }
}